<?php

require_once 'BaseTaxonomy.php';
class NestedTaxonomy extends BaseTaxonomy
{
    public $sortButtonUrl="Yii::app()->controller->createUrl('updateOrder')";
    public $updateButtonUrl="Yii::app()->controller->createUrl('updateTerm', array('id'=>\$data['id']))";
    public $deleteButtonUrl="Yii::app()->controller->createUrl('deleteTerm', array('id'=>\$data['id']))";
    public $statusButtonUrl="Yii::app()->controller->createUrl('statusTerm', array('id'=>\$data['id']))";
    
    public function init()
    {
        parent::init();
        $this->sortButtonUrl = $this->evaluateExpression($this->sortButtonUrl);
    }
    
    public function run()
    {
        $data = $this->getData();
        $this->render('nested', array('data'=>$data));
    }
    
    protected function renderNested($data)
    {
        $baseGridViewScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/gridview';
        foreach ($data as $index => $item) {
            $hasChild = isset($item['children']) && is_array($item['children']) && count($item['children']);
            echo '<li class="sortable" id="items-'.$item['id'].'">';
            $this->render('_item', array('class'=>'odd', 'model'=>$item,'baseGridViewScriptUrl'=>$baseGridViewScriptUrl, 'hasChild'=>$hasChild));
            if ($hasChild) {
                echo '<ul>';
                $this->renderNested($item['children']);
                echo '</ul>';
            }
            echo '</li>';
        }
    }
}